package cn.wh.xwm.controller.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Transaction;

import net.sf.json.JSONObject;

import cn.wh.xwm.controller.action.vo.CustomerView;
import cn.wh.xwm.controller.action.vo.UserInfoView;
import cn.wh.xwm.hibernate.dao.Customerinfo;
import cn.wh.xwm.hibernate.dao.CustomerinfoDAO;
import cn.wh.xwm.hibernate.dao.Userinfo;
import cn.wh.xwm.hibernate.dao.UserinfoDAO;

/**
 * 客户管理 ACTION
 * @author swimming
 *
 */
public class CustomerAction extends BaseAction {
	private String customername;
	private String customerlevel;
	private String customerstatus;
	private String defineId;
	private String id;
	
	private String rows;
	private String page;
	
	private String q_customername;
	private String q_customerlevel;
	
	/**
	 * 查询客户
	 * @throws IOException
	 */
	public void findAllCustomer() throws IOException{
		CustomerinfoDAO dao = new CustomerinfoDAO();
		List<Customerinfo> list = dao.findBypage(page,rows,"Customerinfo");
		List<CustomerView> vlist = new ArrayList<CustomerView>();
		for(Customerinfo info :list){
			CustomerView view = new CustomerView();
			view.setCustomerlevel(info.getLevel());
			view.setCustomername(info.getName());
			view.setCustomerstatus(info.getValid().equals(0)?"无效":"有效");
			view.setId(info.getId());
			vlist.add(view);
		}
		JSONObject object = new JSONObject();
		object.put("rows", vlist);
		object.put("total", dao.findTotalCount("Customerinfo"));
		dao.getSession().close();
		this.responseJson(object);
 	}
	
	/**
	 * 条件查询
	 * @throws IOException
	 */
	public void findByParam() throws IOException{
		CustomerinfoDAO dao = new CustomerinfoDAO();
		List<Customerinfo> list = dao.findByPrame(page,rows,q_customername,q_customerlevel);
		List<CustomerView> vlist = new ArrayList<CustomerView>();
		for(Customerinfo info : list){
			CustomerView view = new CustomerView();
			view.setId(info.getId());
			view.setCustomerlevel(info.getLevel());
			view.setCustomername(info.getName());
			view.setCustomerstatus(info.getValid().equals(0)?"无效":"有效");
			vlist.add(view);
		}
		JSONObject object = new JSONObject();
		object.put("rows", vlist);
		object.put("total", dao.findTotalBysql(q_customername,q_customerlevel));
		dao.getSession().close();
		this.responseJson(object);
	}
	
	/**
	 * 增加客户
	 * @return
	 * @throws IOException 
	 */
	public void addCustomer() throws IOException{
		CustomerinfoDAO dao = new CustomerinfoDAO();
		Customerinfo info = new Customerinfo();
		info.setName(this.getCustomername());
		info.setLevel(this.getCustomerlevel());
		info.setDefineId(this.getDefineId());
		info.setValid(this.getCustomerstatus().equals(1)?1:0);
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.save(info);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}
	
	/**
	 * 修改客户
	 * @throws IOException 
	 */
	public void updateCustomer() throws IOException{
		CustomerinfoDAO dao = new CustomerinfoDAO();
		Customerinfo info = dao.findById(this.getId());
		info.setName(this.getCustomername());
		info.setLevel(this.getCustomerlevel());
		info.setDefineId(this.getDefineId());
		info.setValid(this.getCustomerstatus().equals("1")?1:0);
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.update(info);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}
	
	/**
	 *删除客户
	 * @throws IOException 
	 */
	public void deleteCustomer() throws IOException{
		CustomerinfoDAO dao = new CustomerinfoDAO();
		Customerinfo info = dao.findById(this.getId());
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.delete(info);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}

	public String getCustomername() {
		return customername;
	}

	public void setCustomername(String customername) {
		this.customername = customername;
	}

	public String getCustomerlevel() {
		return customerlevel;
	}

	public void setCustomerlevel(String customerlevel) {
		this.customerlevel = customerlevel;
	}

	public String getCustomerstatus() {
		return customerstatus;
	}

	public void setCustomerstatus(String customerstatus) {
		this.customerstatus = customerstatus;
	}

	public String getDefineId() {
		return defineId;
	}

	public void setDefineId(String defineId) {
		this.defineId = defineId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getQ_customername() {
		return q_customername;
	}

	public void setQ_customername(String q_customername) {
		this.q_customername = q_customername;
	}

	public String getQ_customerlevel() {
		return q_customerlevel;
	}

	public void setQ_customerlevel(String q_customerlevel) {
		this.q_customerlevel = q_customerlevel;
	}


	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}
	
	
}
